﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LibrarySync2.Controls
{
    public class BaseUserControl : UserControl
    {
        protected delegate void UpdateStatusDelegate(TextBox toUpdate, string statusMessage);

        public bool ShowErrorMessage(string message)
        {
            MessageBox.Show(this, message, "OH NOES!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }

        public bool ShowMessage(string message)
        {
            MessageBox.Show(this, message, "A brief message from our sponsors:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return true;
        }

        public static void AddMessage(TextBox textBox, string message)
        {
            //Single-line text boxes can handle only one message, so this is easy.
            if (!textBox.Multiline)
            {
                textBox.Text = message;
                return;
            }

            //Multi-line textboxes need to have the message appended.
            var lines = textBox.Lines;
            var lineList = new List<string>();
            lineList.AddRange(lines);
            lineList.Add(message);
            textBox.Lines = lineList.ToArray();
            textBox.SelectionStart = textBox.TextLength;
            textBox.ScrollToCaret();
        }

        protected void InvokeUpdateStatus(TextBox toUpdate, string statusMessage)
        {
            this.Invoke(new UpdateStatusDelegate(AddMessage), toUpdate, statusMessage);
        }
    }
}
